class TimeslotsController < ApplicationController
  before_filter :authenticate!
  before_filter :protect_from_student, :except => [:search, :request_search, :show]
  before_filter :protect_from_instructor, :only => [:edit, :update]
  before_filter :correct_user, :only => [:destroy]
  # GET /timeslots
  # GET /timeslots.json
  def index
    if @current_user.instructor?
      @timeslots = Timeslot.where("instructor_id = ?", @current_user.usable).order("start_time")
      @timeslots.select! { |t| t.active? || t.unresolved? }
    else
      @timeslots = Timeslot.all
      @timeslots.select! { |t| t.active? && !t.registered? }
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @timeslots }
    end
  end

	# GET /timeslots/search
  # GET /timeslots/search.json
  def search
		if @current_user.student?
			if @current_user.usable.has_active_registration?
				flash.notice = "You already have an active registration."
				redirect_to timeslot_registration_path(@current_user.usable.active_registration.timeslot, @current_user.usable.active_registration)
				return
			end
		end
    @instructors = Instructor.all
		@timeslot = Timeslot.new

    respond_to do |format|
      format.html # search.html.erb
      format.json { render json: @instructors }
    end
  end

	# POST /timeslots/search
  # POST /timeslots/search.json
  def request_search
		@data = params[:timeslot]
		@data[:instructor] = Instructor.find(@data[:instructor])
		@timeslot = Timeslot.new(@data)

    @timeslot = set_time(@timeslot, params[:period])

    @timeslots = Timeslot.where("start_time >= ? AND end_time <= ? AND instructor_id = ?", @timeslot.start_time, @timeslot.end_time, @timeslot.instructor).order("start_time")

		@timeslots.select! { |t| t.active? && !t.registered? }

    respond_to do |format|      
			format.html { render action: "index"}
      format.json { render json: @timeslots }
    end
  end

  # GET /timeslots/1
  # GET /timeslots/1.json
  def show
    @timeslot = Timeslot.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @timeslot }
    end
  end

  # GET /timeslots/new
  # GET /timeslots/new.json
  def new
    @timeslot = Timeslot.build_default

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @timeslot }
    end
  end

  # GET /timeslots/1/edit
  def edit
    @timeslot = Timeslot.find(params[:id])
  end

  # POST /timeslots
  # POST /timeslots.json
  def create
    @timeslot = Timeslot.new(params[:timeslot])
		@timeslot.instructor = @current_user.usable

    respond_to do |format|
      if @timeslot.save
        format.html { redirect_to @timeslot, notice: 'Timeslot was successfully created.' }
        format.json { render json: @timeslot, status: :created, location: @timeslot }
      else
        format.html { render action: "new" }
        format.json { render json: @timeslot.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /timeslots/1
  # PUT /timeslots/1.json
  def update
    @timeslot = Timeslot.find(params[:id])

    respond_to do |format|
      if @timeslot.update_attributes(params[:timeslot])
        format.html { redirect_to @timeslot, notice: 'Timeslot was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @timeslot.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /timeslots/1
  # DELETE /timeslots/1.json
  def destroy
    @timeslot = Timeslot.find(params[:id])
		if @timeslot.registered?
			flash.alert = "Cannot delete timeslot with existing registration."
			redirect_to timeslots_path
			return
		end
    @timeslot.destroy

    respond_to do |format|
      format.html { redirect_to timeslots_url }
      format.json { head :no_content }
    end
  end

  def correct_user
    @timeslot = Timeslot.find(params[:id])
    if @current_user.instructor?
      unless @current_user.usable.id == @timeslot.instructor.id
        redirect_to actions_url, :alert => "Not authorized to view info about this timeslot"
      end
    end
  end

  def set_time(timeslot, period)
    if period == 'Today'
      timeslot.start_time = Time.now
      timeslot.end_time = timeslot.start_time.midnight + 1.day
    elsif period == 'Tomorrow'
      timeslot.start_time = Time.now.midnight + 1.day
      timeslot.end_time = timeslot.start_time + 1.day
    elsif period == 'Day after tomorrow'
      timeslot.start_time = Time.now.midnight + 2.days
      timeslot.end_time = timeslot.start_time + 1.day
    elsif period == '2 next days'
      timeslot.start_time = Time.now
      timeslot.end_time = timeslot.start_time.midnight + 2.days
    elsif period == '3 next days'
      timeslot.start_time = Time.now
      timeslot.end_time = timeslot.start_time.midnight + 3.days
    elsif period == '7 next days'
      timeslot.start_time = Time.now
      timeslot.end_time = timeslot.start_time.midnight + 7.days
    elsif period == '30 next days'
      timeslot.start_time = Time.now
      timeslot.end_time = timeslot.start_time.midnight + 30.days
    elsif period == 'Custom'
      timeslot.end_time = timeslot.end_time + 1.day
    end
    timeslot
  end
end
